package com.xyd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xyd.dao.UserMapper;
import com.xyd.entity.User;
import com.xyd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public Page queryUserByPage(Map<String, Object> cond) {
        Page page = new Page();
        //设置总条数
        page.setTotal(userMapper.getLikeUsersCount(cond));
        //获得每页条数和当前页
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
        page.setPageSize(pageSize);

        int curPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
        page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1)));
        page = PageHelper.startPage(curPageNum, pageSize);
        List<User> list = userMapper.getLikeUsers(cond);

        return page;
    }

    @Override
    public Integer addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public Integer delUser(User user) {
        return userMapper.delUser(user);
    }

    @Override
    public User getUserById(User user) {
        return userMapper.getUserById(user);
    }

    @Override
    public Integer updateUser(User user) {
        return userMapper.updateUser(user);
    }

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
}
